package com.dikeleakagedetection.myapplication.ui.ble

import android.content.Context

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.clj.fastble.BleManager
import com.clj.fastble.data.BleDevice
import com.dikeleakagedetection.myapplication.R

class DeviceAdapter(val context: Context, private val deviceList: ArrayList<BleDevice>) :
    RecyclerView.Adapter<DeviceAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imgBlue: ImageView = itemView.findViewById(R.id.img_blue)
        val txtName: TextView = itemView.findViewById(R.id.tv_device_name)
        val txtMac: TextView = itemView.findViewById(R.id.tv_device_mac)
        val txtRssi: TextView = itemView.findViewById(R.id.tv_rssi)
        val layoutIdle: LinearLayout = itemView.findViewById(R.id.ly_idle)
        val layoutConnected: LinearLayout = itemView.findViewById(R.id.ly_connected)
        val btnDisconnect: Button = itemView.findViewById(R.id.btn_disconnect)
        val btnConnect: Button = itemView.findViewById(R.id.btn_connect)
        //val btnDetail: Button = itemView.findViewById(R.id.btn_detail)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(
            R.layout.item_device_frag_ble_main,
            parent,
            false
        )

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bleDevice= deviceList[position]
        val isConnected = BleManager.getInstance().isConnected(bleDevice)

        holder.txtName.text = bleDevice.name
        holder.txtMac.text = bleDevice.mac
        holder.txtRssi.text = bleDevice.rssi.toString()
        if (isConnected) {
            holder.imgBlue.setImageResource(R.mipmap.ic_blue_connected)
            holder.txtName.setTextColor(-0xe2164a)
            holder.txtMac.setTextColor(-0xe2164a)
            holder.layoutIdle.visibility = View.GONE
            holder.layoutConnected.visibility = View.VISIBLE
        } else {
            holder.imgBlue.setImageResource(R.mipmap.ic_blue_remote)
            holder.txtName.setTextColor(-0x1000000)
            holder.txtMac.setTextColor(-0x1000000)
            holder.layoutIdle.visibility = View.VISIBLE
            holder.layoutConnected.visibility = View.GONE
        }

        holder.btnConnect.setOnClickListener {
            mListener?.onConnect(bleDevice)
        }

        holder.btnDisconnect.setOnClickListener {
            mListener?.onDisConnect(bleDevice)
        }

        /*holder.btnDetail.setOnClickListener {
            mListener?.onDetail(bleDevice)
        }*/
    }

    override fun getItemCount() = deviceList.size

    fun addDevice(bleDevice: BleDevice) {
        removeDevice(bleDevice)
        deviceList.add(bleDevice)
    }

    fun removeDevice(bleDevice: BleDevice) {
        deviceList.remove(bleDevice)
    }

    fun clear() {
        clearConnectedDevice()
        clearScanDevice()
    }

    private fun clearConnectedDevice() {
        val iterator = deviceList.iterator()
        while (iterator.hasNext()){
            if(BleManager.getInstance().isConnected(iterator.next())){
                iterator.remove()
            }
        }
    }

    private fun clearScanDevice() {
        val iterator = deviceList.iterator()
        while (iterator.hasNext()){
            if(!BleManager.getInstance().isConnected(iterator.next())){
                iterator.remove()
            }
        }
    }

    interface OnDeviceClickListener {
        fun onConnect(bleDevice: BleDevice?)
        fun onDisConnect(bleDevice: BleDevice?)
        fun onDetail(bleDevice: BleDevice?)
    }

    private var mListener: OnDeviceClickListener? = null

    fun setOnDeviceClickListener(listener: OnDeviceClickListener?) {
        mListener = listener
    }
}